package com.emellend.api.autocomplete;
import com.emellend.api.R;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.TextView;
/**
 * 当你输入联系人姓名的前一位时，出现联系人的自动提示
 * @author emellend
 *
 */
public class AutoComplete4 extends Activity {
	
	public static final String[] PEOPLE_PROJECTION = new String[] {
		ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autocomplete_4);
		init();
	}

	private void init() {
		AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.edit);
		ContentResolver resolver = this.getContentResolver();
		
		Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, 
				PEOPLE_PROJECTION, null, null, null);
		ContactListAdapter adapter = new ContactListAdapter(this, cursor);
		actv.setAdapter(adapter);
	}
	public static class ContactListAdapter extends CursorAdapter implements Filterable {
		private ContentResolver mContent;
        public ContactListAdapter(Context context, Cursor c) {
            super(context, c);
            mContent = context.getContentResolver();
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            final LayoutInflater inflater = LayoutInflater.from(context);
            final TextView view = (TextView) inflater.inflate(
                    android.R.layout.simple_dropdown_item_1line, parent, false);
            view.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            ((TextView) view).setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        }

        @Override
        public String convertToString(Cursor cursor) {
            return cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        }

        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) {
                return getFilterQueryProvider().runQuery(constraint);
            }

            StringBuilder buffer = null;
            String[] args = null;
            if (constraint != null) {
                buffer = new StringBuilder();
                buffer.append("UPPER(");
                buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
                buffer.append(") GLOB ?");
                args = new String[] { constraint.toString().toUpperCase() + "*" };
            }

            return mContent.query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION,
                    buffer == null ? null : buffer.toString(), args, null);
        }
    }
}
